package com.jellyfishumbrella.GL;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;


public class UberPoly extends AbstractLOD {
	public enum VertexLODTag {
		UNDEFINED, CENTER, INNER, OUTER, BASE
	}

	public MeshGL					parentMesh;
	public Set<Polygon>				polygons	= Collections.synchronizedSet(new HashSet<Polygon>());
	protected ArrayList<Vertex>		allPoints	= new ArrayList<Vertex>();
	public LinkedHashSet<Polygon>	glPolys		= new LinkedHashSet<Polygon>();

	public Surface					surface;

	public UberPoly() {
		this.parentMesh = null;
	}

	public UberPoly(MeshGL parent) {
		this.parentMesh = parent;
	}

	public synchronized void clear() {
		synchronized (this.polygons) {
			this.polygons.clear();
		}
	}

	public void setSurface(String surfaceName) {
		//if (parentMesh != null) {
		//	parentMesh.addSurface(RendererGL.surfaceHash.get(surfaceName));
		//}
		//surface = RendererGL.surfaceHash.get(surfaceName);

		for (Polygon currPoly : this.polygons) {
			surface = parentMesh.setPolySurface(currPoly, surfaceName);
		}
	}

	public void refreshMesh() {
		for(Polygon currPoly : polygons){
			currPoly.calcPolyNormal();
		}
		parentMesh.reUploadVerts = true;
		//parentMesh.refreshVerts = true;
		parentMesh.refreshIndices = true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((polygons == null) ? 0 : polygons.hashCode());
		result = prime * result + ((surface == null) ? 0 : surface.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UberPoly other = (UberPoly) obj;
		if (polygons == null) {
			if (other.polygons != null)
				return false;
		} else if (!polygons.equals(other.polygons))
			return false;
		if (surface == null) {
			if (other.surface != null)
				return false;
		} else if (!surface.equals(other.surface))
			return false;
		return true;
	}
}
